<template>
  <div class="member-order">
    <XtxTabs v-model="activeName" @tab-click="tabClick">
      <XtxTabsPanel v-for="item in orderStatus" :key="item.name" :label="item.label" :name="item.name"></XtxTabsPanel>
    </XtxTabs>
    <div class="order-list">
      <div v-if="loading" class="loading"></div>
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <OrderItem v-for="item in orderList" :key="item.id" :order="item" @on-cancel="handleCancel" @on-delete="handleDelete" @on-confirm="handleConfirm" @on-logistics="handleLogistics" />
    </div>
    <XtxPagination v-if="total > 0" :total="total" :current-page="reqParams.page" :page-size="reqParams.pageSize" @current-change="reqParams.page = $event" />
    <OrderCancel ref="orderCancelCom" />
    <OrderLogistics ref="orderLogisticsCom" />
  </div>
</template>

<script>
import { ref, reactive, watch } from 'vue'
import { orderStatus } from '@/api/constants'
import OrderItem from './components/order-item'
import { findOrderList, deleteOrder, confirmOrder } from '@/api/order'
import OrderCancel from './components/order-cancel'
import OrderLogistics from './components/order-logistics'
import Confirm from '@/components/library/Confirm'
import Message from '@/components/library/Message'
export default {
  name: 'MemberOrder',
  components: {
    OrderItem,
    OrderCancel,
    OrderLogistics
  },
  setup () {
    const activeName = ref('all')
    const orderList = ref([])
    const loading = ref(false)
    const total = ref(0)
    const reqParams = reactive({
      page: 1,
      pageSize: 5,
      orderStatus: 0
    })
    const getOrderList = () => {
      loading.value = true
      findOrderList(reqParams).then(res => {
        loading.value = false
        orderList.value = res.result.items
        total.value = res.result.counts
      })
    }
    watch(reqParams, () => {
      getOrderList()
    }, { immediate: true })
    const tabClick = ({ index }) => {
      reqParams.page = 1
      reqParams.orderStatus = index
    }
    const handleDelete = (order) => {
      Confirm({
        text: '亲，您确认要删除该订单吗？'
      }).then(res => {
        deleteOrder(order.id).then(res => {
          Message({
            type: 'success',
            text: '删除成功'
          })
          getOrderList()
        })
      }).catch(err => {
        console.log(err)
      })
    }
    return {
      activeName,
      orderStatus,
      orderList,
      tabClick,
      loading,
      total,
      reqParams,
      ...useCancel(),
      handleDelete,
      ...useConfirm(),
      ...useLogistics()
    }
  }
}
export const useCancel = () => {
  const orderCancelCom = ref(null)
  const handleCancel = (order) => {
    orderCancelCom.value.open(order)
  }
  return {
    handleCancel,
    orderCancelCom
  }
}
export const useConfirm = () => {
  const handleConfirm = (order) => {
    Confirm({
      text: '亲，您确认收货吗？确认后货款将打给卖家'
    }).then(() => {
      confirmOrder(order.id).then(() => {
        Message({
          type: 'success',
          text: '确认收货成功'
        })
        order.orderState = 4
      })
    }).catch(() => {})
  }
  return {
    handleConfirm
  }
}
export const useLogistics = () => {
  const orderLogisticsCom = ref(null)
  const handleLogistics = (order) => {
    orderLogisticsCom.value.open(order)
  }
  return {
    handleLogistics,
    orderLogisticsCom
  }
}
</script>

<style scoped lang="less">
.member-order {
  height: 100%;
  background-color: #fff;
}
.order-list {
  background: #fff;
  padding: 20px;
  position: relative;
  min-height: 400px;
}
.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255,255,255,.9) url(../../../assets/images/loading.gif) no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>
